<?php
/**
 * 审计处理
 * @author ice<iceinto@mallog.com.cn>
 */
class AuditAction extends CommonAction
{
	public function _initialize() {
		parent::_initialize ();
		$this->assign('nav', 3);
	}

	/**
	 * 审计首页
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function index(){
		$uid = $this->getLoginUserInfo();
		$tasks = D('Queue')->getWaitingAuditList($uid);
		$this->assign('tasks', $tasks);
		$this->display();
	}

	/**
	 * 任务详细
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function task(){
		$id = intval(getGet('id'));
		$uid = $this->getLoginUserInfo();
		if(!$id){
			$this->error(L('_UID_FORMAT_ERROR'));
		}
		$task = M('Task')->find($id);
		//判断当前状态是否满足 审核状态
		$queueStatus = D('Queue')->getStatusById($task['id']);
		if($queueStatus!=6){
			$this->error('当前状态错误！');
		}
		//Get allot
		$taskAllot = D('TaskAllot')->getOneByTid($id, '*');
		if(!$task || !$taskAllot){
			$this->error(L('_DATE_ERROR'));
		}

		if(isPost()){
			$remark = hh(getPost('remark'));
			$audit = hh(getPost('audit'));
			if($audit == L('_PASS')){
				if(empty($remark)){
					$remark = '审核通过！';
				}
				$TaskAuditModel = D('TaskAudit');
					
				if($TaskAuditModel->addOne($uid, $id, $remark)){
					$this->updateStatus($id, 9, $uid);
					$this->success('审核完成！');
				}else{
					$this->error('审核通过失败');
				}

			}else if($audit == L('_RETURN')){
				if(!$remark){
					$this->error(L('_REMARK_MUST'));
				}
				$TaskAuditReturnModel = D('TaskAuditReturn');
				if($this->autoCheckToken()){
					if($TaskAuditReturnModel->addOne($uid, $id, $remark)){
						$this->updateStatus($id, 7, $uid);
					}else{
						$this->error('审核退回失败');
					}
				}
			}else{
				$this->error(L('_DATE_ERROR'));
			}
			$this->redirect('/Audit/');
		}
		$TaskAuditReturnModel = D('TaskAuditReturn');
		$auditReturns = $TaskAuditReturnModel->getOneAllReturns($id);
		if($auditReturns){
			foreach($auditReturns as $key=>$v){
				if($v['image']){
					$auditReturns[$key]['image_url'] = $this->getManagedUrl($v['image'], 'task');
				}
			}
		}
		//Get image
		$taskImage = $this->getManagedUrl($taskAllot['image'], 'task');
		$task = D('Mall')->setAddress($task);
		$this->assign('task', $task);
		$this->assign('taskAllot', $taskAllot);
		$this->assign('taskImage', $taskImage);
		$this->assign('auditReturns', $auditReturns);
		$this->display();
	}

	/**
	 * Update queue status
	 * @param int $tid
	 * @param int $status
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function updateStatus($tid, $status, $auditor = null){
		D('Queue')->updateStatus($tid, $status, $auditor);
	}
}
?>